<?php
/**
 * 2020/9/18
 * Created by breeze
 * User: breeze
 */

namespace app\admin\controller;


use app\admin\validate\AdminUser;
use app\admin\business\AdminUserBus;
use think\Exception;
use think\facade\View;
use think\Session;

class Login extends AdminBase

{
    /**
     * 如果不写 会一直访问adminBase.php中重写的initialize方法
     * 重写initialize方法  防止无限循环跳转
     */
    public function initialize()
    {
        if($this->isLogin()){
            return $this->redirect("index/index");
        }
    }
    public function index(){

        return View::fetch();
    }
    public function md5(){
        echo md5("admin_breeze");

//        echo $this->md5("");
    }
    public function loginCheck(){
        if(!$this->request->isPost()){
            return show(config("status.error"),"请求方式错误");
        }
        //获取请求参数
        $username = $this->request->param("username");
        $password = $this->request->param("password");
        $captcha = $this->request->param("captcha");
        $validateDate = [
            "username" => $username,
            "password" => $password,
            "captcha" => $captcha
        ];
        $validate = new AdminUser();
        if(!$validate->check($validateDate)){
             return show(config("status.error"),$validate->getError());
        }
        $adminUserBus = new AdminUserBus();
        try {
            $result = $adminUserBus->getAdminUserByNameBus($validateDate);
        }catch (\Exception $e){
            return show(config("status.error"),$e->getMessage());
        }
        if ($result){
            return show(config("status.success"),"登录成功了");
        }
        return show(config("status.error"),$validate->getError());
    }
}